import { CryptoJS } from '/cryptojs/lib/tripledes.js'
import { CryptoJS_mode_ECB } from '/cryptojs/lib/mode-ecb.js'
import { SHA1 } from '/cryptojs/lib/SHA1.js'
var comm = require('./comm_util.js')
//3DES字符串加密
export const enCode = (str) => {
  str = String(str)
  if (str == null || str == undefined || str.length <= 0) {
    return "";
  }
  var keyHex = CryptoJS.enc.Utf8.parse(CryptoJS.workFactor);
  var encrypted = CryptoJS.TripleDES.encrypt(str, keyHex, {
    mode: CryptoJS_mode_ECB,
    padding: CryptoJS.pad.Pkcs7  //后端pkcs5填充，前端对应pkcs7
  });
  return encrypted.toString()
};

//3DES字符串解密
export const deCode = (str) => {
  str = String(str)
  if (str == null || str == undefined || str.length<=0){
    return "";
  }
  var keyHex = CryptoJS.enc.Utf8.parse(CryptoJS.workFactor);
  var decrypted = CryptoJS.TripleDES.decrypt(str.replace(/(\r\n|\n|\r)/gm, ""), keyHex, {
    mode: CryptoJS_mode_ECB,
    padding: CryptoJS.pad.Pkcs7 //后端pkcs5填充，前端对应pkcs7
  });
  return decrypted.toString(CryptoJS.enc.Utf8)
};
export const sign = (params)=>{
  var arr = [];
  for (let [k, v] of params) {
    if (v != null && v != undefined) {
      arr.push((k + "=" + v));
    } else {
      arr.push((k + "="));
    }
  }
  var urlStr=arr.sort().join("&");
  return SHA1(urlStr, "chalk01020306".toString()).toString();
};